package www.dao;

import www.domain.Student;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class SqlStudentDao {

    public Student findStudentById(String id)throws Exception{

        String drivername = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String username = "root";
        String password = "root";

        Student student = new Student();
        Class.forName(drivername);
        Connection conn = DriverManager.getConnection(url,username,password);
        Statement stmt = conn.createStatement();
        String sql = "select * from students where id = " + id;

        ResultSet rs = stmt.executeQuery(sql);
        System.out.println(rs);

        while(rs.next()){

            int stu1 = rs.getInt(1);//通过列序号来获取第一行第一列的值，在表中的数据是什么类型，这里获取的就用什么类型来接收
            String stu2 = rs.getString(2);
            String stu3 = rs.getString(3);
            System.out.println(stu1);
            System.out.println(stu2);
            System.out.println(stu3);
            student = new Student();
            student.setId(stu1);
            student.setName(stu2);
            student.setSex(stu3);
        }

        rs.close();
        stmt.close();
        conn.close();
        return student;
    }
}
